package org.abrysov.annoyingkids;

import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements OnClickListener{

	private SoundPool mSoundPool = null;
	
	private int mScream01;
	private int mScream02;
	private int mScream03;
	private int mScream04;
	
    boolean loaded = false;
    int num=0;
	
	private FrameLayout mKid01 = null;
	private FrameLayout mKid02 = null;
	private FrameLayout mKid03 = null;
	private FrameLayout mKid04 = null;
	
	private boolean mKidCrying01 = false;
	private boolean mKidCrying02 = false;
	private boolean mKidCrying03 = false;
	private boolean mKidCrying04 = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //link our volume butt with the app
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        
        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        
        //устанавливаем call-back функцию, которая вызывается по
        //завершению загрузки файла в память
        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        	
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                loaded = true;
                Log.e("Test", "sampleId=" + sampleId + " status=" + status);
            }
        });
        // load screams ))
        mScream01 = mSoundPool.load(this, R.raw.girl_in_medic_room_01  , 1);
        mScream02 = mSoundPool.load(this, R.raw.girl_in_kidroom_02  , 1);
        mScream03 = mSoundPool.load(this, R.raw.scream_03_1  , 1);
        mScream04 = mSoundPool.load(this, R.raw.scream_04_1  , 1);
        
        mKid01 = (FrameLayout)findViewById(R.id.main_fl_kid_01);
        mKid01.setOnClickListener(this);
        
        mKid02 = (FrameLayout)findViewById(R.id.main_fl_kid_02);
        mKid02.setOnClickListener(this);
        
        mKid03 = (FrameLayout)findViewById(R.id.main_fl_kid_03);
        mKid03.setOnClickListener(this);
        
        mKid04 = (FrameLayout)findViewById(R.id.main_fl_kid_04);
        mKid04.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private void scream(int _scream){
    	
		AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float volume = actualVolume / maxVolume;
    	
		if (loaded) {
                mSoundPool.play(_scream, volume, volume, 1, 0, 1f);
        }
    }

	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.main_fl_kid_01:
			
			mKidCrying01 = !mKidCrying01;
			if(mKidCrying01){
				mKid01.setBackgroundResource(R.drawable.bg_cryingkid);
				scream(mScream01);
			}else{
				mKid01.setBackgroundResource(R.drawable.bg_kid);
				mSoundPool.stop(mScream01);
			}
			
			
			break;
			
		case R.id.main_fl_kid_02:
			mKidCrying02 = !mKidCrying02;
			if(mKidCrying02){
				mKid02.setBackgroundResource(R.drawable.bg_cryingkid);
				scream(mScream02);
			}else{
				mKid02.setBackgroundResource(R.drawable.bg_kid);
			}
			
			break;
			
		case R.id.main_fl_kid_03:
			mKidCrying03 = !mKidCrying03;
			if(mKidCrying03){
				mKid03.setBackgroundResource(R.drawable.bg_cryingkid);
				scream(mScream03);
			}else{
				mKid03.setBackgroundResource(R.drawable.bg_kid);
			}
			
			break;
			
		case R.id.main_fl_kid_04:
			mKidCrying04 = !mKidCrying04;
			if(mKidCrying04){
				mKid04.setBackgroundResource(R.drawable.bg_cryingkid);
				scream(mScream04);
			}else{
				mKid04.setBackgroundResource(R.drawable.bg_kid);
			}
			
			break;

		default:
			break;
		}
		
	}

    
}
